<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let p1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('1111');
            },2000)
        })
        let p2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('2222');
            },1000)
        })
        let p3 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('2222');
            },2000)
        })
        
        Promise.race([p1, p2, p3]).then(res => {
            console.log(res);
        })


        arr = [p1, p2, p3]
        // race的实现
        function myRace(arr) {
            return new Promise((resolve, reject) => {
                // arr.forEach(item => {
                //     item.then(res=>{
                //         resolve(res);
                //     },error=> {
                //         reject(error);
                //     })
                // })

                arr.forEach(p => {
                    p.then(resolve,reject);
                })
            })
        }
        function fun(a) {
            console.log('fun',a);
        }
        // setTimeout(fun,1000);
        setTimeout(function() {
            fun(12);
        },1000);
        myRace(arr).then(res => {
            console.log(res,'++==');
        })
    </script>
</body>
</html>